In [7]:
from numpy.linalg import inv
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_blobs
from scipy.linalg import eig
from mpl_toolkits.mplot3d import Axes3D
from diffmaps_util import k, diag
In [8]:
X = np.array([.9,1.1,1.2,1.3]).reshape(2,2)
X = np.array([.9,1.1,1.2,1.3,1.4,1.5,1.6,1.7,1.8]).reshape(3,3)
Out[8]:
In [9]:
%matplotlib inline
In [10]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(X[:,0], X[:,1], X[:,2])
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
plt.show()
$ M = D^{-1}L $
In [11]:
L = k(X, .7)
D = diag(L)
M = inv(D).dot(L)
# Mi,j denotes the transition probability
# from the point xi to the point xj in one time step
print M
Equivalente a
$ M_{i,j}=\frac{k_\epsilon(x_i, x_j)}{p_\epsilon(x_j)} $onde
$ p_\epsilon(x_j) = \sum_i k_\epsilon(x_i, x_j) $
In [12]:
L/L.sum(axis=1).reshape(-1,1)
Out[12]:
$ Ms = D^{1/2}LD^{-1/2} $
In [17]:
Ms = (diag(D,.5)).dot(M).dot(diag(D,-.5))
Ms
Out[17]:
Equivalente a $ Ms = \frac{L_{i,j}}{(d(x_i) \times d(x_j))^{1/2}} $
In [16]:
p = L.sum(axis=1)
for i in range(0,3):
a = []
for j in range(0,3):
a.append(L[i,j]/(p[i]*p[j])**.5)
print a
In [15]:
w, v0, v1 = eig(Ms, left=True)
w = w.real
print '%s\n%s' % (w, v0)
$ P \times \psi_l = \lambda_l \times \psi_l $
In [12]:
Ms.dot(v0)
Out[12]:
In [213]:
w * v0
Out[213]:
In [40]:
w = w[::-1]
In [71]:
phi = v0.T
phi = phi[::-1]
#print w, '\n', phi
print w, '\n', phi
In [202]:
psi = []
for i in range(3):
psi.append([])
for j in range(2):
psi[i].append(phi[j+1,i]/M[i])
psi
Out[202]:
In [204]:
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for p in psi:
ax.scatter(p[0][0],p[0][1],p[0][2])
ax.scatter(p[1][0],p[1][1],p[1][2])
#print p[0][0], p[0][1], p[0][2]
In [75]:
l = w.real[::-1]
print l
psi = v0.T[::-1]
print psi
In [79]:
phi = []
for i in range(3):
phi.append(l[i] * psi[i])
phi
Out[79]:
In [ ]:
pairwise_distances(phi[1:])**2